var rootPath = '';//kk.getRootPath();

/* modelDom */
var modelDom = '\
<form class="form-horizontal" id="modelForm">\
	<div>\
		<input type="hidden" name="id" />\
	</div>\
	<div class="form-group">\
		<label for="name" class="col-sm-2 control-label">名称<span class="text-red">*</span></label>\
		<div class="col-sm-10">\
			<input type="text" class="form-control" id="name" name="name" placeholder="名称">\
		</div>\
	</div>\
	<div class="form-group">\
		<label for="tableName" class="col-sm-2 control-label">表名<span class="text-red">*</span></label>\
		<div class="col-sm-10">\
			<input type="text" class="form-control" id="tableName" name="table_name" placeholder="表名">\
		</div>\
	</div>\
	<div class="form-group">\
		<label for="description" class="col-sm-2 control-label">描述</label>\
		<div class="col-sm-10">\
			<textarea class="form-control" rows="6" id="description" name="description" placeholder="描述"></textarea>\
		</div>\
	</div>\
</form>\
';

/* CRUD 1: Create, 2: Read, 3: Update, 4: Delete */
var CREATE = 1, READ = 2, UPDATE = 3, DELETE = 4;
function CRUDModelFunc(operCode, id, index) {
	var canOpen = false;
	var url;
	var obj;
	
	if (operCode == CREATE) {
		canOpen = true;
	} else if (operCode == READ || operCode == UPDATE) {
		url = rootPath + '/Content/Model/find?id=' + id;
		obj = kk.getObj(url);
		if (obj != null) {
			canOpen = true;
		}
	} else if (operCode == DELETE) {
		url = rootPath + '/Content/Model/delete?id=' + id;
		kk.deleteObj(url, '#table');
	} else {
		return false;
	}
	
	if (!canOpen) {
		return false;
	}
	
	var validator;
	
	xpopup.window({
	    width: 800,
	    content: modelDom,
        buttons: [{
        	id:	'btnOk',
            text: '确定',
            'class': 'btn btn-info btn-flat',
            handler: function ($this) {
    	 		if (validator.form() && doCU(operCode, id, index)) {
            		$this.destroy();
    	 		}
            }
        }, {
        	id:	'btnCancel',
            text: '取消',
            'class': 'btn btn-default btn-flat',
            handler: function ($this) {
        		$this.destroy();
            }
        }],
        afterShow: function ($this) {
			if (operCode == CREATE) {
				$this.setTitle('添加');
			} else if (operCode == READ) {
				$this.setTitle('查看');
				$('#btnOk').hide();
	    		$('#btnCancel').text('关闭');

				$('#modelForm').setForm(obj);

				$('#modelForm').find(':input').attr("disabled", "disabled");		// 习惯性地处理
			} else if (operCode == UPDATE) {
				$this.setTitle('编辑');
				
				$('#modelForm').setForm(obj);
			}

        	validator = $("#modelForm").validate({
    			onfocusout: function(element) {
    				$(element).valid();
    			},
                rules: {
        			name: "required",
        			table_name: "required",
                },
                messages: {
        			name: "请输入名称",
        			table_name: "请输入表名",
                }
            });
        }
	});
}

/* Create & Update */
function doCU(operCode, id, index) {
    var url;
    var method;
	var jsonStr = JSON.stringify($('#modelForm').serializeObject());
	var result;
	
	if (operCode == CREATE) {
		url = rootPath + '/Content/Model/save';
		method = "POST";

		result = kk.doStrAjax(url, method, jsonStr);
	    if (result.code == 0) {
	    	$('#table').bootstrapTable('insertRow', {
				index: 0,
	    		row: result.obj
			});
	    	
	    	return true;
		}
	} else if (operCode == UPDATE) {
		url = rootPath + '/Content/Model/update?id=' + id;
		method = "PUT";

		result = kk.doStrAjax(url, method, jsonStr);
	    if (result.code == 0) {
	    	$('#table').bootstrapTable('updateRow', {
	    		index: index,
	    		row: result.obj
	    	});
	    	
	    	return true;
		}
	}
	
	return false;
}
